#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import *
import tkFileDialog


from PIL import Image
import os, sys, glob, shutil

# encoding=utf8 
import sys
reload(sys) 
sys.setdefaultencoding('utf-8')



def rename_one_file(filePath, maxSize):
    if filePath.endswith(".png"):
        print("Cut One Image:", filePath, maxSize)
        image = Image.open(filePath)
        image.thumbnail(maxSize)
        image.save(filePath)


def renameFolder(rootdir, maxSize):
    if not os.path.exists(rootdir):
        print("null path:", rootdir)
        return

    #列出文件夹下所有的目录与文件
    list = os.listdir(rootdir) 
    for i in range(0,len(list)):
        path = os.path.join(rootdir,list[i])
        if os.path.isfile(path):
            rename_one_file(path, maxSize)
        else:
            renameFolder(path, maxSize)

def runCutImageAlpha():
    ROOT_DIR = curSelectPath.get()
    width = setWidthVar.get()
    height = setHeightVar.get()
    rootPathUtf8 = unicode(ROOT_DIR)
    maxSize = (width, height)
    renameFolder(rootPathUtf8, maxSize)
    print("Resize Completed:", ROOT_DIR, width, "*", height)


ROOT_DIR = "./input"

##不要动
# if __name__ == '__main__':
#     rootPathUtf8 = unicode(ROOT_DIR)
#     renameFolder(ROOT_DIR)


def get_screen_size(window):  
    return window.winfo_screenwidth(),window.winfo_screenheight()  
  
def get_window_size(window):  
    return window.winfo_reqwidth(),window.winfo_reqheight()  
  
def center_window(root, width, height):  
    screenwidth = root.winfo_screenwidth()  
    screenheight = root.winfo_screenheight()  
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)  
    print(size)  
    root.geometry(size)

def selectPath():
    strFolder = tkFileDialog.askdirectory()
    curSelectPath.set(strFolder)

root = Tk()
#root.geometry('400x300+885+465')5
center_window(root, 400, 200)
root.wm_attributes('-topmost',1)

crop_onebyone_v = IntVar()
crop_onebyone_v.set(2)
curSelectPath = StringVar()
setWidthVar = IntVar()
setHeightVar = IntVar()
root.title("缩放图片")
Frame(root)

Label(root,text = "功能：缩放图片").grid(row = 0, column = 0)
Entry(root, textvariable = setWidthVar).grid(row = 1, column = 0)
Entry(root, textvariable = setHeightVar).grid(row = 2, column = 0)
Label(root,text = "目标路径:").grid(row = 3, column = 0)
Entry(root, textvariable = curSelectPath, width = 60).grid(row = 4, column = 0, pady=5)
Button(root, text = "路径选择", command = selectPath).grid(row = 5, column = 0)
Button(root, text = "执行", command = runCutImageAlpha).grid(row = 6, column = 0)


root.mainloop()

